package com.cheli.managerAsset.service.impl;

import com.cheli.commonutils.JwtUtils;
import com.cheli.managerAsset.entity.AsAsset;
import com.cheli.managerAsset.entity.AssetStaff;
import com.cheli.managerAsset.entity.frontvo.AssetStaffVo;
import com.cheli.managerAsset.entity.vo.AssetStaffBackVo;
import com.cheli.managerAsset.mapper.AssetStaffMapper;
import com.cheli.managerAsset.service.AsAssetService;
import com.cheli.managerAsset.service.AssetStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 资产员工关联表 服务实现类
 * </p>
 *
 * @author cheli
 * @since 2021-04-19
 */
@Service
public class AssetStaffServiceImpl extends ServiceImpl<AssetStaffMapper, AssetStaff> implements AssetStaffService {

    @Autowired
    private AsAssetService asAssetService;

    @Autowired
    private AssetStaffService assetStaffService;

    @Override
    public void outAsset(AssetStaffVo assetStaffVo) {
        AssetStaff assetStaff = new AssetStaff();
        BeanUtils.copyProperties(assetStaffVo,assetStaff);
        Map<String, Object> map = new HashMap();
        map.put("staff_id",assetStaffVo.getStaffId());
        map.put("asset_id", assetStaffVo.getAssetId());
       List<AssetStaff> assetStaffs = baseMapper.selectByMap(map);
        if (assetStaffs.size() == 0) {
            baseMapper.insert(assetStaff);
        }else {
            assetStaff.setId(assetStaffs.get(0).getId());
            baseMapper.updateById(assetStaff);
        }
        AsAsset asset = asAssetService.getById(assetStaff.getAssetId());
       asset.setIsOut(1);
        asset.setCount(asset.getCount() + 1);
        asAssetService.updateById(asset);
    }

    @Override
    public List<AssetStaffBackVo> getBackAssetList(String id) {
        return baseMapper.getBackAssetList(id);
    }

    @Override
    public void backAsset(String uid, String id) {
        AssetStaff assetStaff = assetStaffService.getById(id);
        assetStaff.setStatus("eding");
        String assetId = assetStaff.getAssetId();
        assetStaffService.updateById(assetStaff);
    }

    @Override
    public List<AssetStaffBackVo> getAssetStaffList() {
        return baseMapper.getAssetStaffList();
    }
}
